///*
// * To change this template, choose Tools | Templates
// * and open the template in the editor.
// */
//package notificationcenter.ejb;
//
//import entities.Candidate;
//import entities.Company;
//import entities.Notification;
//import entities.NotificationType;
//import entities.Search;
//import facades.CandidateFacade;
//import facades.NotificationFacade;
//import facades.NotificationTypeFacade;
//import java.io.IOException;
//import java.net.URISyntaxException;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.Properties;
//import javax.ejb.Asynchronous;
//import javax.ejb.Stateless;
//import javax.ejb.TransactionManagement;
//import javax.ejb.TransactionManagementType;
//import javax.enterprise.event.Event;
//import javax.enterprise.event.Observes;
//import javax.inject.Inject;
//import mail.EmailEJB;
//import mail.EmailTemplateHelper;
//import mail.datamodel.IdDataSource;
//import qualifiers.CreatedEntityEvent;
//import qualifiers.NotificationEmail;
//import qualifiers.ThrowNotification;
//import sqlgenerators.search.SearchSQLGenerator;
//import sqlgenerators.search.SearchSqlGeneratorInterface;
//import util.DateConverter;
//import util.EncryptTool;
//import util.MessagesHelper;
//
///**
// *
// * @author Alvaro
// */
//@TransactionManagement(value=TransactionManagementType.BEAN)
//@Stateless
//public class NotificationNewSearchEjb {
//    
//    @Inject
//    private CandidateFacade candidateFacade;
//    @Inject
//    private NotificationFacade notificationFacade;
//    @Inject
//    private NotificationTypeFacade notificationTypeFacade;
//    @Inject
//    @CreatedEntityEvent
//    private Event<Notification> createdNotificationEvent;
//    @Inject
//    @ThrowNotification
//    Event<Notification> throwNotificationEvent;
//    @Inject
//    @NotificationEmail
//    Event<Notification> notificationEmail;
//    private SearchSqlGeneratorInterface sqlGenerator;
//    @Inject
//    EmailEJB emailEJB;
//    
//    public NotificationNewSearchEjb(){
//        sqlGenerator=new SearchSQLGenerator();
//    }
//    
//    @Asynchronous
//    public void onNewSearch(@Observes @CreatedEntityEvent Search search) throws IOException, URISyntaxException, Exception{
//        if(search.getIsPublic()){
//
//            String sql=sqlGenerator.getSqlFromSearch(search);
//            List<Candidate> candidateList = candidateFacade.getSearchCandidates(sql, search.getCompany());
////            List<Candidate>candidateList= new ArrayList <Candidate>();
////            candidateList.add(candidateFacade.find(1));//demi
////            candidateList.add(candidateFacade.find(2));//pichon
////            candidateList.add(candidateFacade.find(3102));//me
//            for(Candidate candidate:candidateList){
//                Properties properties= MessagesHelper.getPropertyFromCandidate(candidate);
//
//                StringBuilder mensajeNotificacion=new StringBuilder();
//
//                mensajeNotificacion.append(properties.getProperty("Congratulations"));
//                mensajeNotificacion.append(" ");
//                if(search.getCompany().getCompanyConfiguration().getShowName()){
//                    mensajeNotificacion.append(search.getCompany().getName());
//                }
//                else{
//                    mensajeNotificacion.append(properties.getProperty("NoNameCompany"));
//                }
//                mensajeNotificacion.append(" ");
//                mensajeNotificacion.append(properties.getProperty("PreselectecNewSearch"));
//                mensajeNotificacion.append(" ");
//                mensajeNotificacion.append(DateConverter.convertDateToString(new Date()));
//
//                Notification notification=new Notification();
//                notification.setIsNotificated(false);
//                notification.setDescription(mensajeNotificacion.toString());
//                notification.setDate(new Date());
//                notification.setUserSender(search.getCompany().getIdUser());
//                notification.setUser(candidate.getUser());
//                NotificationType notificationType;
//                if(search.getIsPublic()){
//                    notificationType= notificationTypeFacade.find(5);
//                }
//                else{
//                    notificationType= notificationTypeFacade.find(6);
//                }
//                notification.setNotificationType(notificationType);
//                notificationFacade.create(notification);
//                createdNotificationEvent.fire(notification);
//                throwNotificationEvent.fire(notification);
//                if(notification.getUser().getCandidate().getCandidateConfiguration()==null){
//                    notificationEmail.fire(notification);
//                }
//                else if(notification.getUser().getCandidate().getCandidateConfiguration().getEmailNewSearch()){
//                    notificationEmail.fire(notification);
//                }
//                if(notification.getNotificationType().getIdNotificationType()==5){
//                    String subject="TheRecruitery. "+getSubjectNewSearchEmail(notification.getUserSender().getCompany(), notification.getUser().getCandidate());
//                    String html=EmailTemplateHelper.getHtmlHeader()+EmailTemplateHelper.getHtmlDivText(getSubjectNewSearchEmail(notification.getUserSender().getCompany(), notification.getUser().getCandidate())+".")+htmlPublicNewSearchEmail(notification.getUserSender().getCompany(),notification.getUser().getCandidate(),search);
//                    List<IdDataSource> dataSources=new ArrayList<IdDataSource>();
//                    dataSources.add(new IdDataSource(EmailTemplateHelper.getFileDataSourceFromURL(Thread.currentThread().getContextClassLoader().getResource("emailLogo.png")), EmailTemplateHelper.logoId));
//                    emailEJB.sendHtmlMail(notification.getUser().getEmail(), subject, html, dataSources);
//                }
//
//            }
//        }
//    }
//
//     
//    public String getSubjectNewSearchEmail(Company company,Candidate candidate) throws IOException{
//        Properties properties= MessagesHelper.getPropertyFromCandidate(candidate);
//
//        StringBuilder mensajeNotificacion=new StringBuilder();
//
//        mensajeNotificacion.append(properties.getProperty("Congratulations"));
//        mensajeNotificacion.append(" ");
//        if(company.getCompanyConfiguration().getShowName()){
//            mensajeNotificacion.append(company.getName());
//        }
//        else{
//            mensajeNotificacion.append(properties.getProperty("NoNameCompany"));
//        }
//        mensajeNotificacion.append(" ");
//        mensajeNotificacion.append(properties.getProperty("PreselectecNewSearch"));
//    return mensajeNotificacion.toString();
//    }
//         
//    public String getURLNewSearch(Candidate candidate,Search search) throws Exception{
//        Properties properties= MessagesHelper.getPropertyFromCandidate(candidate);
//        EncryptTool encrypTool= new EncryptTool();
//        String candidateId=encrypTool.encrypt(candidate.getIdCandidate().toString());
//        String url="http://www.therecruitery.com/ofertadeempleo/"+search.getDescription().replace(" ", "-")+"/"+candidateId.replace("/", "-")+"/"+search.getIdSearch();
//        return "<A HREF=\""+url+"\">"+properties.getProperty("ThisJobOffer")+"</A>";
//    }
//    
//         public String htmlPublicNewSearchEmail(Company company,Candidate candidate,Search search) throws IOException, Exception{
//         Properties properties= MessagesHelper.getPropertyFromCandidate(candidate);
//         String companyName;
//         if(company.getCompanyConfiguration().getShowName()){
//            companyName=" "+company.getName()+" ";
//         }
//         else{
//             companyName=" "+properties.getProperty("TheCompany")+" ";
//         }
//         StringBuilder body=new StringBuilder();
//         body.append(EmailTemplateHelper.getHtmlDivText(properties.getProperty("PreselectecNewSearch1")+companyName+properties.getProperty("PreselectecNewSearch2")));
//         body.append(EmailTemplateHelper.getHtmlDivText(properties.getProperty("SelectedInOffer")+" "+getURLNewSearch(candidate, search)));
//         body.append(EmailTemplateHelper.getHtmlDivText(properties.getProperty("PreselectecNewSearch3")));
//         body.append(EmailTemplateHelper.getHtmlDivText(properties.getProperty("PreselectecNewSearch4")));
//         body.append(EmailTemplateHelper.getHtmlDivText(EmailTemplateHelper.getSocialNetworkFooter()));
//         return body.toString();
//     }
//}
